package leetcode_901_1000;

import javafx.scene.effect.SepiaTone;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class LeeCode_939_slow {
    public static void main(String[] args) {

    }
    private static int minAreaRect(int[][] points) {
        Map<Integer, Set<Integer>> map = new HashMap<>();
        for (int[] point : points) {
            if (map.containsKey(point[0])){
                map.get(point[0]).add(point[1]);
            }else {
                Set<Integer> set = new HashSet<>();
                set.add(point[1]);
                map.put(point[0], set);
            }
        }
        int n = points.length, ans = Integer.MAX_VALUE;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                int[] p1 = points[i], p2 = points[j];
                int max_x = Math.max(p1[0], p2[0]);
                int min_x = Math.min(p1[0], p2[0]);
                int max_y = Math.max(p1[1], p2[1]);
                int min_y = Math.min(p1[1], p2[1]);
                int c1 = max_x - min_x;
                int c2 = max_y - min_y;
                if (c1 == 0 || c2 == 0){
                    continue;
                }
                int[] xs = new int[]{min_x, max_x};
                int[] ys = new int[]{min_y, max_y};
                boolean flag = true;
                for (int k = 0; k < 2; k++) {
                    if (!flag){
                        break;
                    }
                    for (int l = 0; l < 2; l++) {
                        int x = xs[k], y = ys[l];
                        if (!map.containsKey(x) || !map.get(x).contains(y)){
                            flag = false;
                            break;
                        }
                    }
                }
                if (flag){
                    ans = Math.min(ans, c1 * c2);
                }
            }
        }
        return ans == Integer.MAX_VALUE ? 0 : ans;
    }
}
